package eu.tolu.cableSpotter.help;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import eu.tolu.cableSpotter.DataOptions;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;

public class AppPreferences {
	private static final String APP_SHARED_PREFS = "eu.tolu.wakeboardspotter.preferences"; // Name
	private static final String APP_SHARED_PREFS_CUSTOME = "eu.tolu.wakeboardspotter.preferencescustome"; // Name
																							// of
																							// the
																							// file
																							// -.xml
	private SharedPreferences appSharedPrefs;
	private SharedPreferences appSharedPrefsCustome;
	private Editor prefsEditor;
	private Editor prefsEditorCustome;
	private Context contextMain;

	private ArrayList<ArrayList<String>> cableArray = new ArrayList<ArrayList<String>>();

	public AppPreferences(Context context) {
		this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS,
				Activity.MODE_PRIVATE);
		this.appSharedPrefsCustome = context.getSharedPreferences(APP_SHARED_PREFS_CUSTOME,
				Activity.MODE_PRIVATE);
		this.prefsEditor = appSharedPrefs.edit();
		this.prefsEditorCustome = appSharedPrefsCustome.edit();
		this.contextMain = context;
	}

	public ArrayList<ArrayList<String>> getStoredCableArray() {
		Integer numb = Integer
				.parseInt(appSharedPrefs.getString("number", "0"));
		Integer numbCustome = Integer
				.parseInt(appSharedPrefsCustome.getString("number", "0"));
		cableArray.clear();

		for (int i = 0; i <= numb; i++) {
			String cabDataString = appSharedPrefs.getString(String.valueOf(i),
					"");
			System.out.println(cabDataString);
			ArrayList<String> cabDat = new ArrayList<String>(
					Arrays.asList(cabDataString.split("###")));
			this.cableArray.add(cabDat);
		}
		
		for (int i = 0; i <= numbCustome; i++) {
			String cabDataString = appSharedPrefsCustome.getString(String.valueOf(i),
					"");
			System.out.println(cabDataString);
			ArrayList<String> cabDat = new ArrayList<String>(
					Arrays.asList(cabDataString.split("###")));
			this.cableArray.add(cabDat);
		}
		
		return cableArray;
	}
	
	public ArrayList<ArrayList<String>> getStoredCustomeCableArray() {

		ArrayList<ArrayList<String>> cableCustomeArray = new ArrayList<ArrayList<String>>();
		
		Integer numbCustome = Integer
				.parseInt(appSharedPrefsCustome.getString("number", "0"));
		
		for (int i = 0; i <= numbCustome; i++) {
			String cabDataString = appSharedPrefsCustome.getString(String.valueOf(i),
					"");
			System.out.println(cabDataString);
			ArrayList<String> cabDat = new ArrayList<String>(
					Arrays.asList(cabDataString.split("###")));
			cableCustomeArray.add(cabDat);
		}
		
		return cableCustomeArray;
	}
	
	public ArrayList<String> getCustomeCableByName(String name) {

		Integer numbCustome = Integer
				.parseInt(appSharedPrefsCustome.getString("number", "0"));
		
		for (int i = 0; i <= numbCustome; i++) {
			String cabDataString = appSharedPrefsCustome.getString(String.valueOf(i),
					"");
			ArrayList<String> cabDat = new ArrayList<String>(
					Arrays.asList(cabDataString.split("###")));
			if(cabDat.size() > 1 && cabDat.get(1).equals(name))
				return cabDat;
		}
		
		return null;
	}

	public boolean buildCableArray() {

		BuildCableArray bCA = new BuildCableArray();
		try {
			cableArray = bCA.getConnectedArrays(this.contextMain);
			setCableArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public ArrayList<ArrayList<String>> getCableArrayByCountry(String country) {
		ArrayList<ArrayList<String>> countryCables = new ArrayList<ArrayList<String>>();
		ArrayList<ArrayList<String>> allCables = getStoredCableArray();
		for (ArrayList<String> cab : allCables) {
			if (cab.size() > 0 && cab.get(0).equals(country))
				countryCables.add(cab);
		}

		return countryCables;

	}

	public ArrayList<String> getCountryArray() {
		ArrayList<String> countryArray = new ArrayList<String>();
		ArrayList<ArrayList<String>> cableArray = getStoredCableArray();
		for (ArrayList<String> cab : cableArray) {
			if (cab.size()>0 && !countryArray.contains(cab.get(0)))
				countryArray.add(cab.get(0));
		}
		Collections.sort(countryArray);
		return countryArray;
	}

	public void setCableArray() throws IOException {

		prefsEditor.putString("number", String.valueOf(cableArray.size()));
		prefsEditor.commit();
		int runInteger = 0;
		for (ArrayList<String> cab : cableArray) {
			prefsEditor
					.putString(String.valueOf(runInteger), listToString(cab));
			System.out.println("SETCABLEARRAY:" + cab.get(1));
			prefsEditor.commit();
			runInteger++;
		}
	}
	
	public void setCustomeCableArrayAll(ArrayList<ArrayList<String>> customeCable) throws IOException {

		prefsEditorCustome.putString("number", String.valueOf(customeCable.size()));
		prefsEditorCustome.commit();
		int runInteger = 0;
		for (ArrayList<String> cab : customeCable) {
			prefsEditorCustome
					.putString(String.valueOf(runInteger), listToString(cab));
			prefsEditorCustome.commit();
			runInteger++;
		}
	}
	
	public void setCustomeCableArray(ArrayList<String> customeCable) throws IOException {
		
		ArrayList<ArrayList<String>> customeCableArray = getStoredCustomeCableArray();
		Boolean isNewCable = true;
		
		for(ArrayList<String> cable : customeCableArray){
			if(cable.size()>1){
				if(cable.get(1).equals(customeCable.get(1))){
					isNewCable = false;
					cable = customeCable;
					setCustomeCableArrayAll(customeCableArray);
				}
			}
		}
		
		if(isNewCable){
			Integer numb = Integer
					.parseInt(appSharedPrefsCustome.getString("number", "0"));
			prefsEditorCustome.putString("number", String.valueOf(numb+1));
			prefsEditorCustome.commit();

			prefsEditorCustome
						.putString(String.valueOf(numb+1), listToString(customeCable));
			prefsEditorCustome.commit();
		}
	}

	private String listToString(ArrayList<String> cabList) {
		String arrStri = "";

		for (String stt : cabList) {
			arrStri += stt + "###";
		}
		return arrStri;
	}

	public void clearPreferences() {
		prefsEditor.clear();
		prefsEditor.commit();
		Context context = this.contextMain.getApplicationContext();
		CharSequence text = "Database cleared";
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}
	
	public void clearCustomePreferences() {
		prefsEditorCustome.clear();
		prefsEditorCustome.commit();
		Context context = this.contextMain.getApplicationContext();
		CharSequence text = "Custome database cleared";
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}
}